<?php

/**
 * View Edit Contatto - LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    16-mag-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {

	var validator = $("#contactForm").validate({
	    invalidHandler: function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
		    $("li#errorLi").show();
		} else {
		    $("li#errorLi").hide();
		};
	    },
	    
	    // Assegna classe 'error' al <li> dell'elemento invalido
	    errorPlacement: function(error, element) {
		//console.log(element);
		element.parent("span").parent("div").parent("li").addClass("error");
		error.appendTo( element.parent("span").parent("div"));
	    },
	    submitHandler: function(form) {
		//$('#salva').attr('disabled', 'disabled');
		form.submit();
	    },	    
	    errorElement: "p",
	    groups: {
		contact_name : "contact_firstname contact_lastname",
		contact_phone: "contact_phone_prefix contact_phone_number",
		contact_work: "contact_work_prefix contact_work_number",
		contact_mobile: "contact_mobile_prefix contact_mobile_number",
		contact_fax: "contact_fax_prefix contact_fax_number",
		address: "contact_city contact_street contact_street_number contact_loc contact_prov contact_cap"
	      },
	    rules: {
		contact_firstname:{
		    required: true
		},
		contact_lastname: {
		    required: true
		},
		contact_email: {
		  required: true,
		  email: true
		},
		contact_phone_prefix: {
		    required: true,
		    digits: true,
		    rangelength: [1, 4]
		},
		contact_phone_number: {
		    required: true,
		    digits: true,
		    rangelength: [1, 10]
		},
		/* Info Opzionali */
		contact_email2: {
		  email: true
		},
		contact_email3: {
		  email: true
		},
		contact_work_prefix: {
		    digits: true,
		    rangelength: [1, 4]
		},
		contact_work_number: {
		    digits: true,
		    rangelength: [1, 10]
		},
		contact_mobile_prefix: {
		    digits: true,
		    rangelength: [1, 4]
		},
		contact_mobile_number: {
		    digits: true,
		    rangelength: [1, 10]
		},
		contact_fax_prefix: {
		    digits: true,
		    rangelength: [1, 4]
		},
		contact_fax_number: {
		    digits: true,
		    rangelength: [1, 10]
		},
		contact_cap:{
		    digits: true,
		    rangelength: [5, 5]
		},
		contact_city:{
		    required: {
			depends: function(element) {
			    return ($('#contact_street').val() != '' || 
				    $('#contact_street_number').val() != '' );
			}
		    }
		},
		contact_prov:{
		    required: {
			depends: function(element) {
			    return ($('#contact_street').val() != '' || 
				    $('#contact_street_number').val() != '' ||
				    $('#contact_city').val() != '');
			}
		    }
		}		
	    },
	    messages: {
		contact_firstname: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		contact_lastname: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		contact_email : {
		    required: "<?php echo  ERR_EMPTY ?>",
		    email: "<?php echo  ERR_EMAIL ?>"
		},
		contact_phone_prefix: {
		    required: "<?php echo  ERR_EMPTY ?>",
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Principale' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Principale' )?>"
		},
		contact_phone_number: {
		    required: "<?php echo  ERR_EMPTY ?>",
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Principale' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Principale' )?>"
		},
		/* Info Opzionali */
		contact_email2 : {
		    email: "<?php echo  ERR_EMAIL ?>"
		},
		contact_email3 : {
		    email: "<?php echo  ERR_EMAIL ?>"
		},
		contact_work_prefix: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Lavoro' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Lavoro' )?>"
		},
		contact_work_number: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Lavoro' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Lavoro' )?>"
		},
		contact_mobile_prefix: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Privato' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Privato' )?>"
		},
		contact_mobile_number: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Privato' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Tel. Privato' )?>"
		},
		contact_fax_prefix: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>"
		},
		contact_fax_number: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'Fax' )?>"
		},
		contact_cap: {
		    digits : "<?php echo   sprintf( ERR_FORMAT, 'CAP' )?>",
		    rangelength : "<?php echo   sprintf( ERR_FORMAT, 'CAP' )?>"
		},	
		contact_street: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		contact_city: {
		    required: "<?php echo  ERR_EMPTY ?>"
		},
		contact_prov: {
		    required: "<?php echo  ERR_EMPTY ?>"
		}
	    },
	    debug:true
	});
    });
    
    $("#salva").click(function() {
      validator.form();
    });
    
    //]]>
</script>

<div class="info">
    <h2>Modifica Contatto</h2>
    <div>Per inserire un nuovo post...</div>
    <div>I campi contrassegnati da <span class="req">*</span> sono obbligatori!</div>
</div>

<form class="fab" name="contactForm" id="contactForm" action="/admin/contatti/edit" method="post" enctype="multipart/form-data">
    <ul>
	<li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none'?>">
	    <h3 id="errorMsgLbl">Si Ã¨ verificato un problema durante l'invio del modulo.</h3>
	    <p id="errorMsg">
		Di seguito trovi evidenziati gli errori riscontrati.
	    </p>
	</li>
	<li class="<?php echo  $this->errorMsg['contact_name'] ? 'error' : '' ?>">
	    <div class="float-left">
		<label class="desc <?php echo  $this->errorMsg['contact_name'] ? 'error' : '' ?>">Nome<span class="req">*</span></label>
		<span class="input">
		    <input id="contact_firstname" name="contact_firstname" class="field text" size="20" type="text" maxlength="255" value="<?php echo  $this->data['contact_firstname'] ?>" />
		    <label>Nome</label>
		</span>
		    
		<span class="input">
		    <input id="contact_lastname" name="contact_lastname" class="field text" size="20" type="text" maxlength="255" value="<?php echo  $this->data['contact_lastname'] ?>" />
		    <label>Cognome</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo  $this->errorMsg['contact_name'] ? 'display:block' : '' ?>"><?php echo  $this->errorMsg['contact_name'] ?></p>
	</li>

	<li class="<?php echo $this->errorMsg['contact_email'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_email'] ? 'error' : ''?>">Email Principale<span class="req">*</span></label>
	    <div>
		<span>
		    <input id="contact_email" name="contact_email" class="field text medium" type="text" maxlength="255" value="<?php echo $this->data['contact_email']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_email'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_email']?></p>
	</li>
	
	<li class="<?php echo $this->errorMsg['contact_phone'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_phone'] ? 'error' : ''?>">Tel. Principale<span class="req">*</span></label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text" id="contact_phone_prefix" name="contact_phone_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['contact_phone_prefix']?>" /> / 
		    <label>Pref</label>
		</span>

		<span class="input">
		    <input class="field text" id="contact_phone_number" name="contact_phone_number" type="text" size="10" value="<?php echo $this->data['contact_phone_number']?>" /> 
		    <label>Numero</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_phone'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_phone']?></p>
	</li>
	
	<li class="section">
	    <h3>Altre Informazioni di Contatto</h3>
	    <div id="instruct3">Inserisci le informazioni facoltative del contatto</div>
	</li>
	
	<li class="<?php echo $this->errorMsg['contact_email2'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_email2'] ? 'error' : ''?>">Email 2</label>
	    <div>
		<span>
		    <input id="contact_email2" name="contact_email2" class="field text medium" type="text" maxlength="255" value="<?php echo $this->data['contact_email2']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_email2'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_email2']?></p>
	</li>
	
	<li class="<?php echo $this->errorMsg['contact_email3'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_email3'] ? 'error' : ''?>">Email 3</label>
	    <div>
		<span>
		    <input id="contact_email3" name="contact_email3" class="field text medium" type="text" maxlength="255" value="<?php echo $this->data['contact_email3']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_email3'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_email3']?></p>
	</li>	
	
	<li class="<?php echo $this->errorMsg['contact_work'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_work'] ? 'error' : ''?>">Tel. Lavoro</label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text" id="contact_work_prefix" name="contact_work_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['contact_work_prefix']?>" /> / 
		    <label>Pref</label>
		</span>

		<span class="input">
		    <input class="field text" id="contact_work_number" name="contact_work_number" type="text" size="10" value="<?php echo $this->data['contact_work_number']?>" /> 
		    <label>Numero</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_work'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_work']?></p>
	</li>
	
	<li class="<?php echo $this->errorMsg['contact_mobile'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_mobile'] ? 'error' : ''?>">Tel. Privato</label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text" id="contact_mobile_prefix" name="contact_mobile_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['contact_mobile_prefix']?>" /> / 
		    <label>Pref</label>
		</span>

		<span class="input">
		    <input class="field text" id="contact_mobile_number" name="contact_mobile_number" type="text" size="10" value="<?php echo $this->data['contact_mobile_number']?>" /> 
		    <label>Numero</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_mobile'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_mobile']?></p>
	</li>

	<li class="<?php echo $this->errorMsg['contact_fax'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_fax'] ? 'error' : ''?>">Fax</label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text" id="contact_fax_prefix" name="contact_fax_prefix" type="text" size="4" maxlength="4" value="<?php echo $this->data['contact_fax_prefix']?>" /> / 
		    <label>Pref</label>
		</span>

		<span class="input">
		    <input class="field text" id="contact_fax_number" name="contact_fax_number" type="text" size="10" value="<?php echo $this->data['contact_fax_number']?>" /> 
		    <label>Numero</label>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_fax'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_fax']?></p>
	</li>

	<li class="section">
	    <h3>Altre Info</h3>
	</li>
	
	<li class="complex <?php echo $this->errorMsg['contact_address'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_address'] ? 'error' : ''?>">Indirizzo</label>
	    <div>
		<span class="input">
		    <input class="field text addr" name="contact_street" id="contact_street" size="50" type="text" value="<?php echo $this->data['contact_street']?>" />
		    <label>Via</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="contact_street_number" id="contact_street_number" size="2" type="text" value="<?php echo $this->data['contact_street_number']?>" />
		    <label>N&deg;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="contact_loc" id="contact_loc" size="50" type="text" value="<?php echo $this->data['contact_loc']?>" />
		    <label>Localit&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="contact_city" id="contact_city" size="50" type="text" value="<?php echo $this->data['contact_city']?>" />
		    <label>Citt&aacute;</label>
		</span>
		<span class="input">
		    <input class="field text addr" name="contact_cap" id="contact_cap" size="10" type="text" value="<?php echo $this->data['contact_cap']?>" />
		    <label>CAP</label>
		</span>

		<span class="left">
		    <select class="field select addr" name="contact_prov" id="contact_prov">
			<option value=""></option>
			<?php foreach( $this->province AS $k => $provincia ): ?>
			<option value="<?php echo $k?>" <?php echo ( $this->data['contact_prov'] == $k ) ? 'selected="selected"' : ''?>><?php echo $provincia['long_name']?></option>
			<?php endforeach; ?>
		    </select>
		    <label>Provincia</label>
		</span>
		<p class="error" style="<?php echo $this->errorMsg['contact_address'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_address']?></p>
	    </div>
	</li>
	
	<li class="section">
	    <h3></h3>
	</li>	
	
	<li class="<?php echo $this->errorMsg['contact_notes'] ? 'error' : ''?>">
	    <label class="desc <?php echo $this->errorMsg['contact_notes'] ? 'error' : ''?>">Note</label>
	    <div>
		<span>
		    <textarea id="contact_notes" class="field textarea small" name="contact_notes" rows="3" cols="100"><?php echo  htmlentities( $this->data['contact_notes'] ) ?></textarea>
		</span>
	    </div>
	    <p class="error" style="<?php echo $this->errorMsg['contact_notes'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['contact_notes']?></p>
	</li>
	<input id="id" name="id" type="hidden" value="<?php echo $this->data['contact_id']?>" />
    </ul>
    <div id="formButtons">
	<button id="salva" type="submit" name="salva" value="salva" class="button positive">
	    <img src="/images/admin/tick.png" alt="" />Salva
	</button>
    </div>
</form>
